import GCJ_CLASS_TYPE = require('./../../common/GCJ_CLASS_TYPE');
import Serializer = require('./../base/Serializer');
import SJGReplaceItemBase = require('./SJGReplaceItemBase');
import SJGReplaceItemImage = require('./SJGReplaceItemImage');
import SJGReplaceItemText = require('./SJGReplaceItemText');
import CommonModel = require('./../../common/model/CommonModel');


var CONFIG=require('../../../../common/config');
export  = SJGSubGroupImageTextOld;

/**
 *
 * @author 
 *
 */
class SJGSubGroupImageTextOld extends SJGReplaceItemBase{
    public image: SJGReplaceItemImage=new SJGReplaceItemImage();
    public text: SJGReplaceItemText=new SJGReplaceItemText();
    
    public constructor(g?) {
        super(g);

        this.ct = GCJ_CLASS_TYPE.CLASS_TYPE_SJGSubGroupImageTextOld;
        this.type=SJGReplaceItemBase.TYPE_SUBGROUP_IMAGETEXT_OLD;
    }
    
    // 从json对象(注意不是json字符串)中解析
    public fromJson(jsonObj,versionId) {
        super.fromJson(jsonObj,versionId);

        if (jsonObj.image != null) {
            this.image.fromJson(jsonObj.image,versionId);
        }
        
        if (jsonObj.text != null) {
            this.text.fromJson(jsonObj.text,versionId);
        }
    }
    
    // 转成json字符串
    public toJson(): string {
        // 去掉父json字符串的头尾
        var superJson = super.toJson();
        superJson = superJson.substr(1,superJson.length - 2);

        var ret = '{';
        ret += superJson;
        
        // image
        ret += ',';
        ret += '"image":' + this.image.toJson();
        
        // text
        ret += ',';
        ret += '"text":' + this.text.toJson();

        ret += '}';
        return ret;
    }
    
    // 转换成CommonModel树
    public toCommonModel(level: number): CommonModel {
        var ret: CommonModel=CommonModel.from(this.id,this.getObjetName(),CommonModel.SPREAD_COLLAPSE,level);
        ret.addChild(this.image.toCommonModel(level+1));
        ret.addChild(this.text.toCommonModel(level+1));
        
        return ret;
    }
    // 获取对象名称
    public getObjetName(): string {
        return "图文";
    }
    // 修改“可替换item”的id
    // 参数：updateOnce［可选］，true表示只发生一次替换
    public updateReplaceItemId(oldId,newId,updateOnce?: Boolean): Boolean {
        // 先处理子，再处理自身
        if(this.image.updateReplaceItemId(oldId,newId,updateOnce)){
            return true;
        }
        if(this.text.updateReplaceItemId(oldId,newId,updateOnce)) {
            return true;
        }
        if(super.updateReplaceItemId(oldId,newId,updateOnce)) {
            return true;
        }

        return false;
    }


//SJG_MARK_APPEND--------------------
    public isContentEmpty():boolean {
        return this.image.isContentEmpty() && this.text.isContentEmpty();
    }
}//SJG_MARK_END-------------------------